﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_News : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "select * from NewsType ";
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                DropDownList1.Items.Add(new ListItem(dr["Type"].ToString(), dr["NTID"].ToString()));
            }
            DropDownList1.Items.Insert(0, "请选择...");
            con.Close();
            Bind();
        }
    }
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select News.NewsID,News.title,NewsType.Type,News.pubdate from News,NewsType where News.TypeId=NewsType.NTID ";
        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        this.Label1.Text = "共有"+ds.Tables[0].Rows.Count+"条记录";
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string NewsID = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
        Response.Redirect("NewsView.aspx?NewsID="+NewsID+"");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con.Open();
            string sql = "select News.NewsID,News.title,NewsType.Type,News.pubdate from News,NewsType where News.TypeId=NewsType.NTID and TypeId="+Convert.ToInt32(DropDownList1.SelectedItem.Value.ToString())+"";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            this.Label1.Text = "共" + ds.Tables[0].Rows.Count + "条新闻";
            con.Close();
        }
        catch
        {
            Response.Write("<script>alert('请选择类别！')</script>");
        }
    }
    //public int TypeId()
    //{
    //    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
    //    con.Open();
    //    string sql = "select Typeid from News";
    //    SqlCommand com = new SqlCommand(sql, con);
    //    SqlDataReader dr = com.ExecuteReader();
    //    dr.Read();
    //    int i = dr["Typeid"].ToString();
    //    return i;

    //}
}
